package com.weijing.pretty_app.util

import android.app.Activity
import android.content.Context

object BrightnessManager {
    private const val KEY_BRIGHTNESS = "brightness"

    // 默认亮度值为 50%
    private var appBrightness: Float = 0.5f

    // 获取保存的亮度值
    fun getSavedBrightness(context: Context): Float {
        return PrefsUtil.getFloat(context, KEY_BRIGHTNESS, -1f) // 默认值为50%亮度
    }

    // 保存亮度值到 SharedPreferences
    fun saveBrightness(context: Context, brightness: Float) {
        PrefsUtil.putFloat(context, KEY_BRIGHTNESS, brightness).apply { }
    }

    // 设置应用全局亮度
    fun setAppBrightness(brightness: Float) {
        appBrightness = brightness.coerceIn(0f, 1f)  // 确保亮度在0到1之间
    }

    // 设置当前 Activity 的亮度
    fun setScreenBrightness(activity: Activity, brightness: Float) {
        val layoutParams = activity.window.attributes
        layoutParams.screenBrightness = brightness
        activity.window.attributes = layoutParams
    }

    // 恢复应用的亮度（适用于每个 Activity）
    fun applyToActivity(activity: Activity) {
        val brightness = getSavedBrightness(activity)
        setScreenBrightness(activity, brightness)
    }
}